package com.darrenchan.leetcode;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-01-25 09:33
 */
public class Q354 {
    public int maxEnvelopes(int[][] envelopes) {
        if (envelopes == null || envelopes.length == 0) {
            return 0;
        }

        Arrays.sort(envelopes, (o1, o2) -> {
            if(o1[0] == o2[0]){
                return o1[1] - o2[1];
            } else {
                return o1[0] - o2[0];
            }
        });

        int len = envelopes.length;
        int[] dp = new int[len];
        int max = 0;

        for (int i = 0; i < len; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if(envelopes[j][0] < envelopes[i][0] && envelopes[j][1] < envelopes[i][1]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            if(max < dp[i]) {
                max = dp[i];
            }
        }

        return max;
    }
}
